import random

from django.shortcuts import render, redirect, reverse, HttpResponse
from django.views import View
# 导入模型表
from book.models import Book, Publish, Author, AuthorDetail,UserInfo

# Create your views here.

login_dict = {}
def home(request,*args,**kwargs):
    # （一）校验当前用户是否登录成功
    try:
        # 尝试从请求中提取登录信息：
        login_dict = request.login_dict
    except Exception as e:
        login_dict = {}
    username = login_dict.get('username')
    # 从UserInfo模型中查询用户名（username）字段匹配给定username值的第一条记录
    user_obj = UserInfo.objects.filter(username=username).first()
    # （二）查询图书信息，按推荐级别渲染图书信息
    book_all = Book.objects.all().order_by("recommend")
    # 渲染并返回HTML模板
    # 函数被传递给render的第三个参数，这意味着当前作用域中的所有局部变量（包括login_dict）都会作为模板的上下文变量可用。这允许在模板中直接访问login_dict的值。
    return render(request,'base.html', locals())

def backend(request,*args,**kwargs):
    publish_all = Publish.objects.all()
    color = random.choice ["primary","success","info","warning","danger"]
    return render(request,'backend.html', locals())